﻿@{
    ViewBag.Title = "CouponBatch";
}

@section scripts
{  
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}
    <form id="form1" runat="server" method="post" style="height: 100%">
    
        <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
            <table width="1000px;">
                <tr>
                    <td width="1000px;">
                        <a class="mini-button" id="save" name="save" iconcls="icon-save" onclick="SaveInfo" plain="true">保存</a>
                        <span class="separator"></span>
                        <a class="mini-button" id="close" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>
        </div>
            <table style="margin-top:25px;margin-left:20px;border-collapse: collapse;">
                <tr>
                    <td width="70px" height ="35px" align="right">礼券分类：</td>
                    <td width="200px" height ="35px" align="left">
                             <table>
                                <tr>
                                    <td width="80px">
                                        <input id="FCouponNumber" name="FCouponNumber" class="mini-buttonedit" style="width:80px;" 
                                            onbuttonclick="getCouponItems('')" onvaluechanged ="getCouponValuesChanged" selectOnFocus="true" />
                                    </td>
                                    <td width="110px">
                                        <input id="FCouponName" name="FCouponName" class="mini-textbox" style="width:110px;" background-color: #CCCCCC;" 
                                            enabled = "false"  align="left" readonly="readonly"/>
                                    </td>
                                </tr>
                            </table>
                    </td>
                    <td width="80px" height ="35px" align="right">礼券类型：</td>
                    <td width="200px" height ="35px" align="left">
                        <input id="FCouponType" name="FCouponType" class="mini-combobox" style="width:200px;" TabIndex="6" value ="1" data ="dCouponType"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="35px" align="right">领券开始：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-datepicker" name="FActStartTime" format="yyyy-MM-dd" enabled="true" style="width:200px;"  value = ""/>
                        <input class="mini-datepicker" name="FStartTime" format="yyyy-MM-dd" enabled="false" style="width:0px;"  value = ""/>
                    </td>
                    <td width="80px" height ="35px" align="right">领券结束：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-datepicker" name="FActEndTime" format="yyyy-MM-dd" enabled="true" style="width:200px;"  value = ""/>
                        <input class="mini-datepicker" name="FEndTime" format="yyyy-MM-dd" enabled="false" style="width:0px;"  value = ""/>
                    </td>
                </tr> 
                <tr>
                    <td width="80px" height ="35px" align="right">首字符：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-textbox" name="FCouSendNoSt1" style="width: 200px;" onvaluechanged="doValueChanged"/>
                    </td>
                    <td width="80px" height ="35px" align="right">起始编码：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-textbox" name="FCouSendNoSt2" style="width: 200px; " onvaluechanged="doValueChanged"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="35px" align="right">结尾字符：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-textbox" name="FCouSendNoSt3" style="width: 200px; " onvaluechanged="doValueChanged" />
                    </td>
                    <td width="80px" height ="35px" align="right">终止编码：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-textbox" name="FEndCode" readonly="true" style="width: 200px; " onvaluechanged="doValueChanged" />
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="35px" align="right">制作张数：</td>
                    <td width="200px" height ="35px" align="left">
                        <input changeonmousewheel="false" class="mini-spinner" maxvalue="99999999" minvalue="1" name="Qty" 
                            showbutton="false" style="width:200px;" onvaluechanged="doValueChanged" />
                    </td>
                    <td width="80px" height ="35px" align="right">消费密码：</td>
                    <td width="200px" height ="35px" align="left">
                        <input name="FPassword" class="mini-password" value="" style="width:200px;"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="35px" align="right">多少签到积分可换：</td>
                    <td width="200px" height ="35px" align="left">
                        <input class="mini-textbox" name="FIntegral" style="width: 200px; " />
                    </td>
                    <td width="80px" height ="35px" align="right">绑定会员：</td>
                    <td width="200px" height ="35px" align="left">
                        @Html.Partial("~/Views/Modular/LookupMember.cshtml")
                    </td>
                </tr>
            </table>
         <input name="FItemID" class="mini-hidden" />
        <input name="FAmount" class="mini-hidden" />

</form>

<script>
    var dCouponType = [{ "id": "1", "text": "满减券" },
                   { "id": "2", "text": "折扣券" },
                { "id": "3", "text": "体验券" }];

    function FormShown() {
        mini.parse();
    }

    function doValueChanged(e) {
        var st1 = mini.getbyName("FCouSendNoSt1").getValue();
        var st2 = mini.getbyName("FCouSendNoSt2").getValue();
        var st3 = mini.getbyName("FCouSendNoSt3").getValue();
        var qty = mini.getbyName("Qty").getValue();
        var noS = "";
        for (var i = 0; i < st2.length; i++) {
            var no = Number(st2.substring(i)).toString();
            if (no != "NaN") {
                var sNo = Number(no);
                var eNo = Number(no) + qty - 1;
                if ((st2.length - i) >= eNo.toString().length) {
                    noS = st2.substring(0, i) + st2.substring(i).replace(st2.substring(st2.length - eNo.toString().length), eNo.toString());
                }
                else if ((st2.length - i) < eNo.toString().length) {
                    noS = st2.substring(0, i) + eNo.toString();
                }
                break;
            }
        }
        mini.getbyName("FEndCode").setValue(st1 + noS + st3);
    }

    function SetInfoData(data) {
        if (data.action == "new") {
            NewInfo();
        }
    }

    function NewInfo() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FCouponNumber").setValue("");
        mini.getbyName("FCouponNumber").setText("");
        mini.getbyName("FCouponName").setValue("");
        mini.getbyName("FCouSendNoSt1").setValue("0");
        mini.getbyName("FCouSendNoSt2").setValue("");
        mini.getbyName("FPassword").setValue("");
        mini.getbyName("FCouSendNoSt3").setValue("");
        mini.getbyName("Qty").setValue("");
        mini.getbyName("FEndCode").setValue("");
        var current = new Date();
        mini.getbyName("FStartTime").setValue(current);
        mini.getbyName("FActStartTime").setValue(current);
        current.setFullYear(current.getFullYear() + 1)
        mini.getbyName("FEndTime").setValue(current);
        mini.getbyName("FActEndTime").setValue(current);
    }

    function validate() {
        if (mini.getbyName("FCouponNumber").getValue() == '') {
            mini.alert('礼券类型未选择');
            return false;
        }
        if (mini.getbyName("FCouSendNoSt2").getValue() == '') {
            mini.alert('起始编码不能为空');
            return false;
        }
        if (mini.getbyName("Qty").getValue() == '' || mini.getbyName("Qty").getValue() <= 0) {
            mini.alert('制作张数不能为空,必须大于0');
            return false;
        }
        return true;
    }

    function SaveInfo() {
        mini.getbyName("save").disable();
        var form = new mini.Form("#form1");
        if (validate() == false) {
            mini.getbyName("save").enable();
            return;
        }
        var data = form.getData(true, false);
        data.mSystemAppID = parent.window.getSystemAppID();
        var json = mini.encode(data);
        $.post("SaveBatchCouponSend", { mjson: json }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                NewInfo();
            }
            else {
                mini.alert(ret.error);
            }
        });
        mini.getbyName("save").enable();
    }

    function getCouponItems(mSearchKey) {
        mini.open({
            url: "../GetItem/Index",
            title: "礼券选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "Coupon", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                mini.get("FCouponNumber").setValue(action[0].FItemID);
                mini.get("FCouponNumber").setText(action[0].FNumber);
                mini.get("FCouponName").setValue(action[0].FName);
                getCouponDate(action[0].FItemID);
            }
        });
    }

    function getCouponValuesChanged() {
        var sSearchKey = mini.get("FCouponNumber").getText();
        var mAppID = parent.window.getSystemAppID();
        if (sSearchKey == "") return;
        var sTbName = "Coupon";
        var json = "[{";
        json = json + "'FSystemAppID':" + "'" + mAppID + "',";
        json = json + "'FTableCode':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FNumber':" + "'" + sSearchKey + "'";
        json = json + "}]";
        $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
            var sdata = mini.decode(text);
            if (sdata.data.length == 1) {
                mini.get("FCouponNumber").setValue(sdata.data[0].FItemID);
                mini.get("FCouponNumber").setText(sdata.data[0].FNumber);
                mini.get("FCouponName").setValue(sdata.data[0].FName);
                var strjson = "[{'FItemID':'" + sdata.data[0].FItemID + "','mSystemAppID':'" + mAppID + "'}]";
                $.post("../Coupon/GetCoupon", { json: strjson }, function (data) {
                    var mdata = mini.decode(data);
                    mini.getbyName("FAmount").setValue(mdata.data[0].FValue);
                });
                getCouponDate(sdata.data[0].FItemID);
            }
            else {
                mini.get("FCouponNumber").setValue("");
                mini.get("FCouponNumber").setText("");
                mini.get("FCouponName").setValue("");
            }
        });
    }

    function getCouponDate(CouponID) {
        $.post("GetCoupon", { json: "[{'FItemID':'" + CouponID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            var current = new Date();
            mini.getbyName("FStartTime").setValue(current);
            if (mdata.data[0].FDateType == 0 || mdata.data[0].FDateType == null) {
                current.setFullYear(current.getFullYear() + 10);
                mini.getbyName("FEndTime").setValue(current);
            } else if (mdata.data[0].FDateType == 1) {
                var dayTime = mdata.data[0].FDays * 24 * 60 * 60 * 1000; //参数天数的时间戳
                var nowTime = current.getTime(); //当天的时间戳
                var t = new Date(nowTime + dayTime).toLocaleString(); //把两个时间戳转换成普通时间
                mini.getbyName("FEndTime").setValue(t);
            } else if (mdata.data[0].FDateType == 1) {
                mini.getbyName("FEndTime").setValue(mdata.data[0].FEndDate);
            }
        });
    }

</script>